#删除注释工具
import tkinter as tk
from tkinter import filedialog, messagebox
import os

class POCommentRemover:
    def __init__(self, root):
        self.root = root
        self.root.title("PO文件注释删除工具")
        self.root.geometry("600x400")
        
        # 创建主框架
        self.main_frame = tk.Frame(root, padx=20, pady=20)
        self.main_frame.pack(expand=True, fill='both')
        
        # 文件选择部分
        self.file_frame = tk.Frame(self.main_frame)
        self.file_frame.pack(fill='x', pady=(0, 20))
        
        self.file_label = tk.Label(self.file_frame, text="PO文件路径：")
        self.file_label.pack(side='left')
        
        self.file_path = tk.StringVar()
        self.file_entry = tk.Entry(self.file_frame, textvariable=self.file_path, width=50)
        self.file_entry.pack(side='left', padx=5)
        
        self.browse_button = tk.Button(self.file_frame, text="浏览", command=self.browse_file)
        self.browse_button.pack(side='left')
        
        # 操作按钮
        self.button_frame = tk.Frame(self.main_frame)
        self.button_frame.pack(pady=20)
        
        self.process_button = tk.Button(self.button_frame, text="删除注释", command=self.remove_comments)
        self.process_button.pack(side='left', padx=10)
        
        # 状态显示
        self.status_frame = tk.Frame(self.main_frame)
        self.status_frame.pack(fill='x', pady=20)
        
        self.status_label = tk.Label(self.status_frame, text="状态：等待选择文件")
        self.status_label.pack(anchor='w')
        
    def browse_file(self):
        filename = filedialog.askopenfilename(
            title="选择PO文件",
            filetypes=[("PO files", "*.po"), ("All files", "*.*")]
        )
        if filename:
            self.file_path.set(filename)
            self.status_label.config(text="状态：已选择文件")
    
    def remove_comments(self):
        file_path = self.file_path.get()
        if not file_path:
            messagebox.showerror("错误", "请先选择PO文件！")
            return
            
        if not os.path.exists(file_path):
            messagebox.showerror("错误", "文件不存在！")
            return
            
        try:
            # 读取文件内容
            with open(file_path, 'r', encoding='utf-8') as f:
                lines = f.readlines()
            
            # 过滤掉以"#:"开头的行
            filtered_lines = [line for line in lines if not line.strip().startswith('#:')]
            
            # 创建备份文件
            backup_path = file_path + '.bak'
            with open(backup_path, 'w', encoding='utf-8') as f:
                f.writelines(lines)
            
            # 写入过滤后的内容
            with open(file_path, 'w', encoding='utf-8') as f:
                f.writelines(filtered_lines)
            
            removed_count = len(lines) - len(filtered_lines)
            self.status_label.config(text=f"状态：成功删除 {removed_count} 行注释，原文件已备份为 {backup_path}")
            messagebox.showinfo("成功", f"已成功删除 {removed_count} 行注释！\n原文件已备份为：{backup_path}")
            
        except Exception as e:
            messagebox.showerror("错误", f"处理文件时出错：{str(e)}")
            self.status_label.config(text="状态：处理失败")

def main():
    root = tk.Tk()
    app = POCommentRemover(root)
    root.mainloop()

if __name__ == "__main__":
    main() 